package com.example.finalsystem.Controller;

import com.alibaba.fastjson.JSON;
import com.example.finalsystem.Entity.Client;
import com.example.finalsystem.Entity.Deal;
import com.example.finalsystem.Entity.ListGoods;
import com.example.finalsystem.Service.ClientService;
import com.example.finalsystem.Service.DealService;
import com.example.finalsystem.Service.ListGoodsService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.util.List;

@RestController
public class ClientController {
    @Autowired
    ClientService clientService;
    @Autowired
    DealService dealService;
    @Autowired
    ListGoodsService listGoodsService;
    private SqlSession openSession()
    {
        String resource = "mybatis-config.xml";
        InputStream is = ClientController.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return   sessionFactory.openSession(true);
    }

    @ResponseBody
    @PostMapping("/client/getSale")
    public String getSale(@RequestParam Long clientId)
    {
        SqlSession sqlSession = openSession();
        double sale =0;
        for (Deal d:dealService.findDealByClientId(clientId)
             ) {
            for (ListGoods l:listGoodsService.getListGoodsByDealId(d.getDealid(),sqlSession)
                 ) {
                sale+= l.getGoodsprice()*l.getGoodsnumber();
            }
        }
        sqlSession.close();
        return sale+"";
    }

    @ResponseBody
    @PostMapping("/client/addClient")
    public String addClient(@RequestParam String clientName)
    {
        return clientService.addClient(clientName);
    }

    //    仓库管理员 确认 出货单 state 设置为已确认   只更改进货数量 其他保持一致
    @ResponseBody
    @PostMapping("/client/fixClient")
    public String fixClient(@RequestParam Long clientId ,@RequestParam String clientName)
    {
        return clientService.fixClient(clientId,clientName);
    }

    @ResponseBody
    @PostMapping("/client/deleteClient")
    public String deleteClient(@RequestParam Long clientId)
    {
        return clientService.deleteClient(clientId);
    }

    @ResponseBody
    @PostMapping("/client/getClient")
    public String getClient(@RequestParam Long clientId)
    {
        return JSON.toJSONString(clientService.getClient(clientId));
    }

    @ResponseBody
    @PostMapping("/client/getAllClient")
    public String getAllClient()
    {
        SqlSession sqlSession = openSession();
        List<Client> clientList =clientService.getAllClient(sqlSession);
        sqlSession.close();
        return  JSON.toJSONString( clientList);
    }
}
